package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.project.domain.BusinessException;
import com.hngates.project.domain.IdName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.ConstructionOrganizationMapper;
import com.hngates.project.domain.ConstructionOrganization;
import com.hngates.project.service.IConstructionOrganizationService;
import com.hngates.common.core.text.Convert;

/**
 * 施工单位Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-23
 */
@Service
public class ConstructionOrganizationServiceImpl implements IConstructionOrganizationService {
    @Autowired
    private ConstructionOrganizationMapper constructionOrganizationMapper;

    /**
     * 查询施工单位
     *
     * @param id 施工单位主键
     * @return 施工单位
     */
    @Override
    public ConstructionOrganization selectConstructionOrganizationById(Long id) {
        return constructionOrganizationMapper.selectConstructionOrganizationById(id);
    }

    /**
     * 查询施工单位列表
     *
     * @param constructionOrganization 施工单位
     * @return 施工单位
     */
    @Override
    public List<ConstructionOrganization> selectConstructionOrganizationList(ConstructionOrganization constructionOrganization) {
        return constructionOrganizationMapper.selectConstructionOrganizationList(constructionOrganization);
    }

    /**
     * 新增施工单位
     *
     * @param constructionOrganization 施工单位
     * @return 结果
     */
    @Override
    public int insertConstructionOrganization(ConstructionOrganization constructionOrganization) {
        constructionOrganization.setCreateTime(DateUtils.getNowDate());
        return constructionOrganizationMapper.insertConstructionOrganization(constructionOrganization);
    }

    /**
     * 修改施工单位
     *
     * @param constructionOrganization 施工单位
     * @return 结果
     */
    @Override
    public int updateConstructionOrganization(ConstructionOrganization constructionOrganization) {
        constructionOrganization.setUpdateTime(DateUtils.getNowDate());
        return constructionOrganizationMapper.updateConstructionOrganization(constructionOrganization);
    }

    /**
     * 批量删除施工单位
     *
     * @param ids 需要删除的施工单位主键
     * @return 结果
     */
    @Override
    public int deleteConstructionOrganizationByIds(String ids) {
        return constructionOrganizationMapper.deleteConstructionOrganizationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除施工单位信息
     *
     * @param id 施工单位主键
     * @return 结果
     */
    @Override
    public int deleteConstructionOrganizationById(Long id) {
        return constructionOrganizationMapper.deleteConstructionOrganizationById(id);
    }

    @Override
    public List<IdName> selectOrganizationName() {

        return constructionOrganizationMapper.selectOrganizationName();
    }

    /**
     * @param organizationList 施工单位列表
     * @param updateSupport    是否支持更新 如果已经存在 则进行更新数据
     * @param operName         操作用户
     * @return
     */

    @Override
    public String importOrganization(List<ConstructionOrganization> organizationList, Boolean updateSupport, String operName) {

        if (Objects.isNull(organizationList) || organizationList.size() == 0) {
            throw new BusinessException("导入施工单位不能为空");
        }
        StringBuilder importMsg = new StringBuilder();

        //循环excel中的记录
        for (ConstructionOrganization constructionOrganization : organizationList) {
            try {

                //constructionContract是循环中 当前的记录对象
                //根据当前的Excel中的对象去数据库中查 查询时 以Excel中名称作为参数去查 返回值是 与项目同名的记录转为数据库对象
                ConstructionOrganization constructionOrganization1 = constructionOrganizationMapper.selectConstructionOrganizationByOrganizationId(constructionOrganization.getOrganizationId());
                //判断数据库对象是否存在
                if (constructionOrganization1 == null) {
                    //如果不存在就创建
                    constructionOrganization1 = new ConstructionOrganization();
                    constructionOrganization1.setOrganizationId(constructionOrganization.getOrganizationId());
                    constructionOrganization1.setUnitId(constructionOrganization.getUnitId());
                    constructionOrganization1.setLeader(constructionOrganization.getLeader());
                    constructionOrganization1.setPhone(constructionOrganization.getPhone());
                    constructionOrganization1.setNativePlace(constructionOrganization.getNativePlace());
                    constructionOrganization1.setPaymentLg(constructionOrganization.getPaymentLg());
                    constructionOrganization1.setLgBalance(constructionOrganization.getLgBalance());
                    constructionOrganization1.setLgDate(constructionOrganization.getLgDate());
                    //赋值完成后 讲对象插入到数据库中
                    constructionOrganizationMapper.insertConstructionOrganization(constructionOrganization1);

                    importMsg.append("<br/>" + "施工单位" + constructionOrganization1.getOrganizationId() + "导入成功");

                } else if (updateSupport) {
                    constructionOrganization1.setUpdateBy(operName);
                    constructionOrganization1.setUnitId(constructionOrganization.getUnitId());
                    constructionOrganization1.setLeader(constructionOrganization.getLeader());
                    constructionOrganization1.setPhone(constructionOrganization.getPhone());
                    constructionOrganization1.setNativePlace(constructionOrganization.getNativePlace());
                    constructionOrganization1.setPaymentLg(constructionOrganization.getPaymentLg());
                    constructionOrganization1.setLgBalance(constructionOrganization.getLgBalance());
                    constructionOrganization1.setLgDate(constructionOrganization.getLgDate());

                    constructionOrganizationMapper.updateConstructionOrganization(constructionOrganization1);

                    importMsg.append("<br/>"  + "施工单位" + constructionOrganization1.getOrganizationId() + "更新成功");
                } else {

                    importMsg.append("<br/>"  + "施工单位" + constructionOrganization1.getOrganizationId() + "已存在");
                }

            } catch (Exception e) {
                importMsg.append("<br/>"  + "施工单位" + constructionOrganization.getOrganizationId() + "导入时异常");
            }

        }

        return importMsg.toString();
    }
}
